package com.service.base_exception;

import com.common.baseException.CustomizeException;
import com.common.baseException.ExceptionEnum;
import com.common.baseModel.JsonResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName GlobalExceptionHandler
 * @Description 全局异常处理器
 * @Author 吃麻椒的花酱
 * @Date 2022/2/9 21:06
 * @Version 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResult<Object> error(Exception e){
        e.printStackTrace();
        return JsonResult.createError(e.getMessage());
    }

    @ExceptionHandler(CustomizeException.class)
    @ResponseBody
    public JsonResult<Object> error(CustomizeException e){
        e.printStackTrace();
        return JsonResult.createError(e.getMsg());
    }

    /***
     * 404处理
     * @param e
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public JsonResult<Object> notFountHandler(HttpServletRequest request, NoHandlerFoundException e){
        return JsonResult.create(ExceptionEnum.NOT_FOUND);

    }

}
